#include "PersonDatabase.h"

#include <QSqlQuery>
#include <QMessageBox>

#include <QDebug>

PersonInfo::PersonInfo()
{
    firstName = "";
    middleName = "";
    lastName = "";
    addressStreet = "";
    addressCity = "";
    addressState = "";
    lastVisit = "";
    birthDate = "";

    addressZip = 0;
}

PersonInfo::PersonInfo(const PersonInfo &info)
{
    firstName = info.firstName;
    middleName = info.middleName;
    lastName = info.lastName;
    addressStreet = info.addressStreet;
    addressCity = info.addressCity;
    addressState = info.addressState;
    lastVisit = info.lastVisit;
    birthDate = info.birthDate;

    addressZip = info.addressZip;
}

PersonDatabase::PersonDatabase()
{
    initDatabase();
}

void PersonDatabase::initDatabase()
{
    DatabaseLock lock(this);
    QSqlQuery query;

    // Check if user_auth table already exists
    query.exec("SELECT name FROM sqlite_master WHERE type='table' AND name='person_info'");

    // If query returns a value, the table already exists and initialization can be skipped
    if (query.next())
        return;

    // Create table to hold user authentication data
    // Fields: userId, username, password, type
    query.exec("CREATE TABLE IF NOT EXISTS person_info ("
               "personId INTEGER PRIMARY KEY AUTOINCREMENT, "
               "userID INTEGER, "
               "firstName TEXT, "
               "middleName TEXT, "
               "lastName TEXT, "
               "addressStreet TEXT, "
               "addressCity TEXT, "
               "addressState TEXT, "
               "addressZip INTEGER, "
               "birthDate TEXT, "
               "lastVisit TEXT)");
}

PersonInfo PersonDatabase::getInfo(int userId)
{
    PersonInfo info;

    // Search for info with userId
    DatabaseLock lock(this);
    QSqlQuery query;

    query.prepare("SELECT * FROM person_info "
                  "WHERE userId=:userId");
    query.bindValue(":userId", userId);
    query.exec();

    // Not found
    if (!query.next())
        return info;

    // Create info record
    info.firstName = query.value("firstName").toString();
    info.middleName = query.value("middleName").toString();
    info.lastName = query.value("lastName").toString();
    info.addressStreet = query.value("addressStreet").toString();
    info.addressCity = query.value("addressCity").toString();
    info.addressState = query.value("addressState").toString();
    info.addressZip = query.value("addressZip").toInt();
    info.birthDate = query.value("birthDate").toString();
    info.lastVisit = query.value("lastVisit").toString();

    return info;
}

bool PersonDatabase::addPerson(PersonInfo *info, int userId)
{
    DatabaseLock lock(this);
    QSqlQuery query;

    query.prepare("INSERT INTO person_info (userId, firstName, middleName, lastName, "
                  "addressStreet, addressCity, addressState, addressZip, "
                  "birthDate, lastVisit) "
                  "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
    query.addBindValue(userId);
    query.addBindValue(QVariant(info->firstName));
    query.addBindValue(QVariant(info->middleName));
    query.addBindValue(QVariant(info->lastName));
    query.addBindValue(QVariant(info->addressStreet));
    query.addBindValue(QVariant(info->addressCity));
    query.addBindValue(QVariant(info->addressState));
    query.addBindValue(info->addressZip);
    query.addBindValue(QVariant(info->birthDate));
    query.addBindValue(QVariant(info->lastVisit));
    query.exec();

    return true;
}
